<template>
	<view class="withdrawal">
		<view class="banner">
			<view class="banner-bg"></view>
			<view class="fix" :style="{ height: CustomBar + 'px' }">
				<view class="header"
					:style="{ height: CustomBar + 'px', background: `rgba(247, 232, 216,${opacity})` }">
					<view class="statusbar" :style="{ height: StatusBar + 'px' }"></view>
					<view class="header-main">
						<view :style="{ width: menuButtonInfo.width + 'px' }">
							<view class="back" @click="handBack">
								<image
									src="https://wuyeshop.oss-cn-beijing.aliyuncs.com/image/20230712/20230712103649_dzrz.png"
									mode=""></image>
							</view>
						</view>
						<view class="title">
							提现
						</view>
						<view :style="{ width: menuButtonInfo.width + 'px' }">

						</view>
					</view>
				</view>
			</view>
			<view class="price-card">
				<template v-if="queryForm.type === 'BALANCE'">
					<view class="label">可提现金额(元)</view>
					<view class="price">
						<text>¥</text>
						<text>{{ userInfo.balance }}</text>
					</view>
				</template>
				<template v-else>
					<view class="label">累计佣金(元)</view>
					<view class="price">
						<text>¥</text>
						<text>{{ userInfo.brokerage || 0 }}</text>
					</view>
				</template>

			</view>
		</view>
		<view class="bank-info">
			<view class="label">提现银行卡</view>
			<view class="place flex align-center" @click="handChooseYhk()">
				<text v-if="bandkCardInfo.id">{{ bandkCardInfo.openingBank }}（{{ bandkCardInfo.newBankCard }}）</text>
				<template v-else-if="userInfo.isBank">
					<view class="tip">选择银行卡</view>
				</template>
				<template v-else>
					<view class="tip">请先绑定银行卡,</view>
					<view class="go">去绑定</view>
				</template>

				<view class="icon">
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
		</view>
		<view class="guide"></view>
		<view class="tixian">
			<view class="nav">提现金额</view>
			<view class="zdy-input">
				<image src="https://wuyeshop.oss-cn-beijing.aliyuncs.com/image/20230712/20230712103416_utiv.png"
					mode=""></image>
				<view class="value">
					<input type="number" v-model="queryForm.balance">
				</view>
				<view class="all" @click="handAll">全部提现</view>
			</view>
			<view class="tips">
				预计2小时内到账
			</view>
		</view>
		<view class="submit">
			<button @click="handSubmit">立即提现</button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad,
		onPageScroll
	} from "@dcloudio/uni-app";
	import {
		useAppsStore
	} from '@/stores/modules/app'
	import {
		doWidth
	} from '@/apis/user.js'
	import {
		storeToRefs
	} from 'pinia'
	const useStore = useAppsStore()
	const {
		getUserInfo: userInfo,
	} = storeToRefs(useStore)
	const {
		doUserInfo
	} = (useStore)


	const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
	const system = uni.getSystemInfoSync()
	const StatusBar = system.statusBarHeight
	let custom = wx.getMenuButtonBoundingClientRect();
	let CustomBar = custom.bottom + custom.top - system.statusBarHeight;



	const opacity = ref(0)
	onPageScroll((e) => {
		const childerOpa = Number((e.scrollTop / 200).toFixed(2))
		opacity.value = childerOpa >= 1 ? 1 : childerOpa
	})

	const handAll = () => {
		if (queryForm.value.type === 'BALANCE') {
			queryForm.value.balance = userInfo.value.balance
		} else {
			queryForm.value.balance = userInfo.value?.brokerage || 0
		}
	}

	const bandkCardInfo = ref({})
	const queryForm = ref({
		balance: '',
		name: '',
		bankCard: '',
		openingBank: '',
		bankId: '',
		payPassword: '',
		type: 'BALANCE'
	})

	const handChooseYhk = () => {
		uni.navigateTo({
			url: '/pagesA/bankCard/index?type=choose',
			events: {
				someEvent: (info) => {
					queryForm.value.bankId = info.id
					queryForm.value.name = info.name
					queryForm.value.bankCard = info.bankCard
					queryForm.value.openingBank = info.openingBank
					bandkCardInfo.value = info
				}
			}
		})
	}
	let loading = false
	const handSubmit = async () => {
		const params = uni.$uv.deepClone(queryForm.value)
		if (!params.bankId) {
			uni.$uv.toast('请选择提现银行卡')
			return
		}
		if (!params.balance) {
			uni.$uv.toast('请输入提现金额')
			return
		}
		if (Number(params.balance) < 1) {
			uni.$uv.toast('金额不能低于1')
			return
		}
		if (loading) return
		loading = true
		try {
			await doWidth(params)
			uni.$uv.toast('已提交申请')
			queryForm.value.balance = ''
			doUserInfo()

		} catch (e) {
			loading = false
		}
	}

	const handBack = () => {
		uni.navigateBack({
			delta: 1
		})
	}
	onLoad((e) => {
		queryForm.value.type = e?.type || 'BALANCE'
	})
</script>

<style lang="scss" scoped>
	.withdrawal {
		min-height: 100vh;
		background-color: #fff;

		.banner {
			position: relative;
			overflow: hidden;
			padding-bottom: 80rpx;

			&-bg {
				position: absolute;
				top: 0;
				width: 750rpx;
				height: 600rpx;
				padding-bottom: 100rpx;
				background: linear-gradient(179deg, #FFC434 0%, #FFF7E4 100%);
			}

			.header {
				display: flex;
				flex-direction: column;
				position: fixed;
				top: 0;
				width: 750rpx;
				z-index: 5;

				&-main {
					flex: 1;
					display: flex;
					align-items: center;
					padding-left: 20rpx;

					.back {
						image {
							width: 18rpx;
							height: 33rpx;
						}
					}

					.title {
						flex: 1;
						text-align: center;
						font-size: 36rpx;
						font-family: PingFang SC-Bold, PingFang SC;
						font-weight: bold;
						color: #333333;
					}
				}
			}

			.price-card {
				position: relative;
				z-index: 3;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				padding: 60rpx 0 40rpx 0;

				.label {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #666666;
				}

				.price {
					margin-top: 8rpx;

					text {
						font-size: 56rpx;
						font-family: PingFang SC-Heavy, PingFang SC;
						font-weight: 800;
						color: #333333;

						&:first-child {
							font-size: 30rpx;
						}
					}
				}
			}
		}

		.bank-info {
			position: relative;
			z-index: 9;
			margin-top: -30rpx;
			border-radius: 20rpx 20rpx 0 0;
			padding: 34rpx 24rpx 40rpx 24rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: #fff;

			.label {
				font-size: 30rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #333333;
			}

			.place {
				font-size: 30rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;

				.tip {
					font-size: 30rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #999999;
				}

				.go {
					font-size: 30rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #F97222;
				}
			}
		}

		.guide {
			height: 16rpx;
			background: #F5F5F5;
		}

		.tixian {
			margin-top: 48rpx;
			padding: 0 24rpx;

			.nav {
				font-size: 30rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #333333;
			}

			.zdy-input {
				margin-top: 34rpx;
				display: flex;
				align-items: center;
				padding-bottom: 20rpx;
				border-bottom: 1rpx solid #EEEEEE;

				image {
					width: 28.63rpx;
					height: 34.14rpx;
				}

				.value {
					flex: 1;
					margin: 0 20rpx;

					input {
						flex: 1;
						font-size: 48rpx;
						font-family: PingFang SC-Bold, PingFang SC;
						font-weight: bold;
						color: #333333;
					}
				}

				.all {
					font-size: 26rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FF8800;
				}
			}

			.tips {
				margin-top: 24rpx;
				font-size: 26rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
			}
		}

		.submit {
			margin-top: 112rpx;
			display: flex;
			justify-content: center;

			button {
				font-size: 32rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
				width: 702rpx;
				height: 100rpx;
				line-height: 100rpx;
				background: #FFC434;
				border-radius: 16rpx;

				&::after {
					border: none;
				}
			}
		}
	}
</style>